<?php

/**
 * Sous-Element de parsing pour les tags de la forme {!...} ou {!!...}
 *
 * @category   McCube
 * @package    Template\Parser\Element
 * @author     Leiha Sellier <leiha.sellier@gmail.com>
 * @license    http://creativecommons.org/licenses/by/3.0/  CC BY 3.0
 * @link       http://code.google.com/p/mechanics-cubes/
 */
class McCube_Template_Parser_Element_Helper_Version1
    extends McCube_Template_Parser_Element_Helper_Abstract{

    protected static $_register = array(
          'Theme' => array('class'=> 'McCube_Theme', 'type'=> 'singleton')
        , 'Html'  => array('class'=> 'McCube_Html' , 'type'=>'static')
    );

    /**
     *
     *
     * @param string $variable
     * @param string $inTag
     * @return string
     */
    public function build($helper, $shelper='', $inTag='', $filters=''){
        $inTag = parent::_parse('\[(.*?)(?<!\\\)]' , '_buildCallbackForArrayIndex', $inTag);
        $inTag = parent::_parse('\((.*?)(?<!\\\)\)', '_buildCallbackForMethodArg' , $inTag);

        switch(self::$_register[$helper]['type']){
            case    'singleton':
                $helper = self::$_register[$helper]['class'].'::get(\''.$shelper.'\')';
            break;
            case    'static'   :
                $helper = self::$_register[$helper]['class'];
                if($shelper){
                    $helper .= '::get'.ucfirst($shelper).'()';
                } else{
                    $inTag = preg_replace('~->~', '::', $inTag, 1);
                }
            break;
        }


        $helper .= $inTag;

         if($filters)
            $helper = parent::_applyFilters($helper, $filters);

        return parent::buildPhpTag($helper);
    }
}